#if IOS || WINDOWS

using System;
using Brain.Rendering;
using System.Runtime.InteropServices;

namespace Brain
{
	public static class iOSUtil
	{
		private static bool initialized = false;
		private static bool isRetina;
		public static bool IsRetina 
		{
			get
			{ 
				return isRetina; 
			}
		}

        private static iOSDevice device;
        public static iOSDevice Device
        {
            get
            {
                return device;
            }
        }

#if IOS
		public static void Initialize(int viewWidth, int viewHeight)
#else
        /// <summary>
        /// 
        /// </summary>
        /// <param name="simulateIpad">If false becomes iPhone</param>
        /// <param name="simulateRetina"></param>
        public static void Initialize(iOSDevice device, bool simulateRetina)
#endif
		{
#if IOS

			if (!initialized) 
			{
				isRetina = (MonoTouch.UIKit.UIScreen.MainScreen.Scale > 1.0);
                if (viewWidth >= 1024)
                {
                    ipad = true;
                }
                isRetina = false;
			}
#else
            
            iOSUtil.device = device;
            isRetina = simulateRetina;
#endif

            initialized = true;
		}

#if IOS
        [DllImport(MonoTouch.Constants.SystemLibrary)]
        static internal extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output, IntPtr oldLen, IntPtr newp, uint newlen);

        public static iOSDevice GetHardwareVersion()
        {
            string HardwareProperty = "hw.machine";

            // get the length of the string that will be returned
            var pLen = Marshal.AllocHGlobal(sizeof(int));
            sysctlbyname(HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0);

            var length = Marshal.ReadInt32(pLen);

            // check to see if we got a length
            if (length == 0)
            {
                Marshal.FreeHGlobal(pLen);
                return iOSDevice.Unknown;
            }

            // get the hardware string
            var pStr = Marshal.AllocHGlobal(length);
            sysctlbyname(HardwareProperty, pStr, pLen, IntPtr.Zero, 0);

            // convert the native string into a C# string
            var hardwareStr = Marshal.PtrToStringAnsi(pStr);
            var ret = iOSDevice.Unknown;

            // determine which hardware we are running
            if (hardwareStr == "iPhone1,1")
                ret = iOSDevice.iPhone2G;
            else if (hardwareStr == "iPhone1,2")
                ret = iOSDevice.iPhone3G;
            else if (hardwareStr == "iPhone2,1")
                ret = iOSDevice.iPhone3Gs;
            else if (hardwareStr == "iPhone3,1")
                ret = iOSDevice.iPhone4;
            else if (hardwareStr == "iPod1,1")
                ret = iOSDevice.iPod1G;
            else if (hardwareStr == "iPod2,1")
                ret = iOSDevice.iPod2G;
            else if (hardwareStr == "iPod3,1")
                ret = iOSDevice.iPod3G;
            else if (hardwareStr == "iPad1,1")
                ret = iOSDevice.iPad1G;
            else if (hardwareStr == "iPad2,1")
                ret = iOSDevice.iPad2G;
            else if (hardwareStr == "iPad3,1")
                ret = iOSDevice.iPad3G;
            else if (hardwareStr == "i386" || hardwareStr == "x86_64" || hardwareStr == "x86_32")
                ret = iOSDevice.Simulator;

            // cleanup
            Marshal.FreeHGlobal(pLen);
            Marshal.FreeHGlobal(pStr);

            return ret;
        }
#endif
	}
}

#endif